package com.study.lintcode.my.code_10;

/**********************************************************************
 * &lt;p&gt;文件名：Example_1.java &lt;/p&gt;
 * &lt;p&gt;文件描述：(https://www.lintcode.com/problem/a-b-problem/)
 * @project_name：LintCode
 * @author zengshunyao
 * @create 2019/1/5 21:56
 * @history
 * @department：政务事业部
 * Copyright ChengDu Funi Cloud Code Technology Development CO.,LTD 2014
 *                    All Rights Reserved.
 */
public class Example_6 {
    /**
     * @param A: sorted integer array A
     * @param B: sorted integer array B
     * @return: A new sorted integer array
     */
    public int[] mergeSortedArray(int[] A, int[] B) {
        // write your code here
        int pa = 0;
        int pb = 0;
        int ab[] = new int[A.length + B.length];
        int pab = 0;
        while (pab != A.length + B.length) {
            while (A.length > pa && B.length > pb && A[pa] <= B[pb]) {
                ab[pab++] = A[pa];
                pa++;
            }
            while (A.length > pa && B.length > pb && A[pa] > B[pb]) {
                ab[pab++] = B[pb];
                pb++;
            }
            //A完了
            if (A.length == pa) {
                while (pab != A.length + B.length) {
                    ab[pab++] = B[pb++];
                }
            }
            //B完了
            if (B.length == pb) {
                while (pab != A.length + B.length) {
                    ab[pab++] = A[pa++];
                }
            }
        }
        return ab;
    }


    public static void main(String[] args) {
        int[] A = new int[]{1, 2, 3, 4, 5, 6};
        int[] B = new int[]{2, 3, 4, 5, 6, 7};
        System.out.println(new Example_6().mergeSortedArray(
                A,
                B));
    }
}
